package com.example.controller;

import com.example.VO.EmpDepVO;
import com.example.entity.Employees;
import com.example.service.EmployeesService;
import com.example.utils.ReturnT;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @description employees
 * @author 48495
 * @date 2025-03-05
 */
@Slf4j
@RestController
@RequestMapping
@Api(tags = "employees", description = "员工相关接口")
public class EmployeesController {

    @Resource
    private EmployeesService employeesService;

    /**
     * 新增
     * @author 48495
     * @date 2025/03/05
     **/
    @PostMapping("/insert")
    @ApiOperation("新增")
    public Object insert(Employees employees){
        log.info("新增");
        return employeesService.insert(employees);
    }

    /**
     * 刪除
     * @author 48495
     * @date 2025/03/05
     **/
    @DeleteMapping("/delete")
    @ApiOperation("刪除")
    public Object delete(int id){
        log.info("刪除");
        return employeesService.delete(id);
    }

    /**
     * 更新
     * @author 48495
     * @date 2025/03/05
     **/
    @PutMapping("/update")
    @ApiOperation("更新")
    public Object update(Employees employees){
        log.info("更新");
        return employeesService.update(employees);
    }

    /**
     * 查询 根据主键 id 查询
     * @author 48495
     * @date 2025/03/05
     **/
    @GetMapping("/load")
    @ApiOperation("查询 根据主键 id 查询")
    public Object load(int id){
        log.info("查询 根据主键 id 查询");
        return employeesService.load(id);
    }

    /**
     * 查询 分页查询
     * @author 48495
     * @date 2025/03/05
     **/
    @GetMapping("/pageList")
    @ApiOperation("查询 分页查询")
    public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int offset,
                                        @RequestParam(required = false, defaultValue = "10") int pagesize) {
        log.info("查询 分页查询");
        return employeesService.pageList(offset, pagesize);
    }

    /**
     * 同时查询员工和与之对应的部门的内容
     * 支持姓名模糊查询和部门code精确查询
     * @param firstName
     * @param lastName
     * @param departmentCode
     * @return
     */
    @GetMapping("/select")
    @ApiOperation(value = "同时查询员工和与之对应的部门的内容")
    @HystrixCommand(fallbackMethod = "selectAllEmpFallback")
    public ReturnT<List<EmpDepVO>> selectAllEmp(String firstName, String lastName, String departmentCode){
        log.info("同时查询员工和与之对应的部门的内容");
        return employeesService.selectAllEmp(firstName, lastName, departmentCode);
    }

    /**
     * Fallback method for handling exceptions during the employee and department query process.
     * Logs the root cause of the exception and returns an error response.
     *
     * @param throwable the exception that triggered the fallback
     * @return an error response with a message indicating a query error
     */
    public ReturnT<List<EmpDepVO>> selectAllEmpFallback(String firstName, String lastName, String departmentCode,Throwable throwable) {
        log.error("[selectAllEmpFallback][exception({})]", ExceptionUtils.getRootCauseMessage(throwable));
        return ReturnT.error("查询错误");
    }

}
